<template>
  <div class="main-container">
    <div class="main-content">
      <div class="row">
        <div class="col-sm-10 col-sm-offset-1">
          <div class="login-container">
            <div class="center">
              <h1>
                <i class="ace-icon fa fa-leaf green"></i>
                <span class="">控台登录</span>
              </h1>
            </div>

            <div class="space-6"></div>

            <div class="position-relative">
              <div id="login-box" class="login-box visible widget-box no-border">
                <div class="widget-body">
                  <div class="widget-main">
                    <h4 class="header blue lighter bigger">
                      <i class="ace-icon fa fa-coffee green"></i>
                      请输入用户名密码
                    </h4>

                    <div class="space-6"></div>

                    <form>
                      <fieldset>
                        <label class="block clearfix">
                          <span class="block input-icon input-icon-right">
                            <input v-model="user.loginName" type="text" class="form-control" placeholder="用户名"/>
                            <i class="ace-icon fa fa-user"></i>
                          </span>
                        </label>

                        <label class="block clearfix">
                          <span class="block input-icon input-icon-right">
                            <input v-model="user.password" type="password" class="form-control" placeholder="密码"/>
                            <i class="ace-icon fa fa-lock"></i>
                          </span>
                        </label>

                        <label class="block clearfix">
                          <span class="block input-icon input-icon-right">
                            <div class="input-group">
                              <input v-model="user.imageCode" type="text" class="form-control" placeholder="验证码">
                              <span class="input-group-addon" id="basic-addon2">
                                <img id="verImg" @click="loadCodeImg()" height="34px"/>
                              </span>
                            </div>
                          </span>
                        </label>

                        <div class="space"></div>

                        <div class="clearfix">
                          <label class="inline">
                            <input v-model="remember" type="checkbox" class="ace"/>
                            <span class="lbl">记住我</span>
                          </label>

                          <button type="button"
                                  class="width-35 pull-right btn btn-sm btn-primary"
                                  v-on:click="login()">
                            <i class="ace-icon fa fa-key"></i>
                            <span class="bigger-110">登录</span>
                          </button>
                        </div>

                        <div class="space-4"></div>
                      </fieldset>
                    </form>

                  </div><!-- /.widget-main -->

                </div><!-- /.widget-body -->
              </div><!-- /.login-box -->
            </div><!-- /.position-relative -->

          </div>
        </div><!-- /.col -->
      </div><!-- /.row -->
    </div><!-- /.main-content -->
  </div><!-- /.main-container -->
</template>

<script>
import Toast from "../../public/static/js/toast";
import Loading from "../../public/static/js/loading";

  export default {
    name: "login",
    data: function() {
      return {
        user: {},
        remember: false, // 默认勾选记住我
        base: `${process.env.VUE_APP_BASE_URL}/system`,
      }
    },
     mounted: function() {
      let _this = this
      $("body").removeClass("no-skin");
      $("body").attr("class", "login-layout light-login");
      _this.loadCodeImg()
    },

    methods: {
      // 登录
      login() {
        let _this = this
        Loading.show();
        _this.$axios.post(`${_this.base}/admin/users/login`, _this.user).then((res)=>{
          Loading.hide();
          if (res.data.data) {
            let user = res.data.data
            SessionStorage.set(SESSION_KEY_USER, user);
            _this.$router.push('/welcome')
          } else {
            _this.user.password = "";
            _this.user.imageCode = ""
            Toast.warning(res.data.msg)
            _this.loadCodeImg()
          }
        })
      },
      loadCodeImg() {
        let _this = this
        _this.$axios.get(`${_this.base}/captcha`).then((res) => {
            _this.user.imageCodeKey = res.data.key
            $("#verImg").attr('src', res.data.image)
        })
      }
    }
  }
</script>

<style scoped>
  .input-group-addon {
    padding: 0;
  }
</style>